import redis
import time

redis_config = {
    "host": "127.0.0.1",   # host参数使用字符串
    "port": 6379,   # 端口号，默认6379
    "password": "",  # 密码
    # "db": 5 # 数据库的名字，5表示第6个数据库
}

# 创建数据的连接池
pool = redis.ConnectionPool(**redis_config)

# 通过连接池创建一个连接
conn = redis.Redis(connection_pool=pool)

# # # 操作string
# # conn.set("name", 'abcdefg')
# # # print(conn.get('name'))  # 获取到字节类型， byte，而不是字符串，需要转码
# # # name = str(conn.get("name"), encoding='utf-8')
# # # print(name)
# #
# # # print(conn.strlen("name"))
# # # print(conn.getrange("name", 1, 3))  # 获取字符串并切片，开始和结束的下标，包含
# # print("现在name的值为：", str(conn.get("name"), encoding='utf-8'))
# # old_value = conn.getset("name2", "坚果")  # 设置指定key的新值，有旧值时返回，没有返回None
# # print("获取name2旧的值为：", old_value)
# # print("name2现在的值为：", str(conn.get("name"), encoding='utf-8'))
#
# # 操作List
# conn.lpush("mylist6661", "baili", "beifan", "weiwei")
# time.sleep(1)
# print(conn.lrange("mylist6661", 0, -1))

# # 操作set
# conn.sadd("myset6661", "one", "two", "three")
# time.sleep(1)
# print(conn.smembers("myset6661"))

# # 操作hash
# conn.hset("myhash6661", "k1", "v1")
# time.sleep(1)
# print(conn.hget("myhash6661", "k1"))
#
# conn.close()
